jQuery(function($) {
	function fmtDate(date) {
		return date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
				+ date.getDate();
	}

	Handlebars.registerHelper("ifnullZero", function(v) {
		if (!v) {
			return 0;
		}
		return v;
	});

	var now = new Date();
	var aday = 24 * 60 * 60 * 1000;
	Handlebars.registerHelper("showday", function(idx) {
		return fmtDate(new Date(now.valueOf() - idx * aday));
	});
	Handlebars.registerHelper("calint", function(v1, operator, v2) {
		switch (operator) {
		case '+':
			return v1 + v2;
		case '-':
			return v1 - v2;
		case '*':
			return v1 * v2;
		case '/':
			return Math.floor(v1 / v2);
		}
	});

	$.get(ctx+"/web/statistics/verinfo.json").success(function(json) {
		var html = $("#ver-tpl").html();
		var tpl = Handlebars.compile(html);
		$("#ver-tpl").after(tpl(json.d));
	});

	$.get(ctx+"/web/statistics/last10info.json").success(function(json) {
		if (json.c == 0) {
			json.d = json.d.reverse();
			var html = $("#last10-tpl").html();
			var tpl = Handlebars.compile(html);
			$("#last10-tpl").after(tpl(json.d));
		}

	});
	$.get(ctx+"/web/statistics/hourinfo.json").success(function(json) {
		if(json.c==0&&json.d){
			var data=json.d;
			json.d={};
			json.d.loop=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23];
			
			var html = $("#hourstat-tpl").html();
			var tpl = Handlebars.compile(html);
			json.d.yesterday=data.slice(0,24);
			json.d.today=data.slice(24);
			$("#hourstat-tpl").after(tpl(json.d));
		}
	});
	$.get(ctx+"/web/statistics/moneyinfo.json").success(function(json) {
		var data = json.d;
		if (data && data.days) {
			var weekalisum = 0;
			var weekalicnt = 0;
			var weekwxsum = 0;
			var weekwxcnt = 0;
			var weekiapsum = 0;
			var weekiapcnt = 0;
			
			var weekiyearlvlcnt=0;
			var weekayearlvlcnt=0;
			var weekrealmoney=0;

			for (var i = 0; i < data.days.length; i++) {
				var item = data.days[i];
				if (i > 0) {
					weekalisum += item.alimoney;
					weekwxsum += item.wxmoney;
					weekiapsum += item.iapmoney;

					weekalicnt += item.alicnt;
					weekwxcnt += item.wxcnt;
					weekiapcnt += item.iapcnt;
					
					weekiyearlvlcnt=item.iyearlvlcnt;
					weekayearlvlcnt=item.ayearlvlcnt;
					weekrealmoney=item.realmoney;
				}

				item.money = item.alimoney + item.wxmoney + item.iapmoney;
				item.cnt = item.alicnt + item.wxcnt + item.iapcnt;
			}
			data.KEY_DAYS_LENGTH=data.days.length;
			data.KEY_WEEK_ALI_AVG = Math.floor(weekalisum / data.days.length);
			data.KEY_WEEK_ALI_CNT = weekalicnt;
			data.KEY_WEEK_WX_AVG = Math.floor(weekwxsum / data.days.length);
			data.KEY_WEEK_WX_CNT = weekwxcnt;
			data.KEY_WEEK_IAP_AVG = Math.floor(weekiapsum / data.days.length);
			data.KEY_WEEK_IAP_CNT = weekiapcnt;
			
			data.KEY_WEEK_MONEY_AVG = data.KEY_WEEK_ALI_AVG + data.KEY_WEEK_WX_AVG+data.KEY_WEEK_IAP_AVG;
			data.KEY_WEEK_CNT = data.KEY_WEEK_ALI_CNT + data.KEY_WEEK_WX_CNT+ data.KEY_WEEK_IAP_CNT;

			data.KEY_WEEK_IYEARLVL_CNT = weekiyearlvlcnt;
			data.KEY_WEEK_AYEARLVL_CNT = weekayearlvlcnt;

			// data.KEY_DAY_CNT=data.KEY_WEEK.length;
			var html = $("#money-tpl").html();
			var tpl = Handlebars.compile(html);
			$("#money-tpl").after(tpl(data));
		}
	});
});